// 引入脚本
import DB from '@/uni_modules/mc-ui/components/m-sqlite/js_sdk/sqlite.js';

// 封装本地数据方法库使用
const UseSqliteFun = function() {
	return {
		isOpen() {
			let open = DB.isOpen();
			return open;
		},
		// 打开数据库
		openSQL(successCallback, errorCallback) {
			// 查询是否已打开数据库
			let open = DB.isOpen();
			if (!open) {
				DB.openSqlite().then(res => {
					if (successCallback) successCallback(res);
					console.log("数据库已打开");
				}).catch(error => {
					if (errorCallback) errorCallback(error);
					console.log("数据库开启失败");
				});
			} else {
				if (successCallback) successCallback(true);
			}
		},
		// 关闭数据库
		closeSQL(successCallback, errorCallback) {
			// 查询是否已打开数据库
			let open = DB.isOpen();
			if (open) {
				DB.closeSqlite().then(res => {
					if (successCallback) successCallback(res);
					console.log("数据库已关闭");
				}).catch(error => {
					if (errorCallback) errorCallback(error);
					console.log("数据库关闭失败");
				});
			}
		},
		creatSqlRow(param, successCallback, errorCallback) {
			let {
				dbTable,
				sql,
			} = param;
			UseSqliteFun.openSQL();
			let open = DB.isOpen();
			if (!open) {
				UseSqliteFun.openSQL();
			}

			// if (open) {
			// 防止数据库开启异常问题
			DB.createTable(dbTable, sql).then((ret) => {
				successCallback && successCallback(ret)
			}).catch(error => {
				errorCallback && errorCallback(error);
			})
			// };
		},
		/**
		 * 向表格里添加数据 or 插入
		 */
		insertSqlRow(param, successCallback, errorCallback) {
			let {
				dbTable,
				sql,
				condition
			} = param;
			DB.insertTableData(dbTable, sql, condition).then((ret) => {
				successCallback && successCallback(ret)
			}).catch(error => {
				errorCallback && errorCallback(error);
			});
		},
		insertSqlRowNew(param, successCallback, errorCallback) {
			let {
				dbTable,
				sql,
				condition
			} = param;

			const sql1 = sql.split(',').map(item => item.trim().replace(/^'|'$/g, ''));
			const condition1 = condition.split(',').map(item => item.trim().replace(/^'|'$/g, ''));
			DB.insertTableDataNew(dbTable, sql1,condition1).then((ret) => {
				successCallback && successCallback(ret)
			}).catch(error => {
				errorCallback && errorCallback(error);
			});

		},
		updateSqlRow(param, successCallback, errorCallback) {
			UseSqliteFun.openSQL();
			let {
				dbTable,
				data,
				lname,
				lvalue
			} = param;
			let open = DB.isOpen();
			if (open) {

				DB.updateTableData(dbTable, data, lname, lvalue).then((ret) => {
					successCallback && successCallback(ret)
				}).catch(error => {
					errorCallback && errorCallback(error);
				});
			}
		},
		deleteSqlRow(param, successCallback, errorCallback) {
			UseSqliteFun.openSQL();
			let {
				dbTable,
				key = null,
				id = null
			} = param;
			let open = DB.isOpen();
			if (open) {
				DB.deleteTableData(dbTable, key, id).then(ret => {
					successCallback && successCallback(ret)
				}).catch(error => {
					errorCallback && errorCallback(error);
				});
			}
		},
		selectTableData(dbTable, uname, namevalue, upass, passvalue, urrn, rrnvalue, customSql) {
			return DB.selectTableData(dbTable, uname, namevalue, upass, passvalue, urrn, rrnvalue, customSql)
		},
		async deleteSql() {
			let open = DB.isOpen();
			if (!open) {
				await DB.openSqlite().then(res => {
					setTimeout(() => {
						DB.deleteSql();
					}, 300);
				}).catch(error => {
					console.log("数据库开启失败");
				});

				DB.closeSqlite()

			} else {
				await DB.deleteSql();
				await DB.closeSqlite()
			}
		},
		insertOrReplaceData(dbTable, data, condition) {
			// 判断有没有传参
			if (dbTable !== undefined && data !== undefined) {
				if (condition == undefined) {
					var sql = `INSERT OR REPLACE INTO ${dbTable} VALUES('${data}')`;
				} else {
					var sql = `INSERT OR REPLACE INTO ${dbTable} (${condition}) VALUES(${data})`;
				}
				// console.log(sql);
				return new Promise((resolve, reject) => {
					// 表格添加数据
					plus.sqlite.executeSql({
						name: this.dbName,
						sql: sql,
						success(e) {
							resolve(e);
						},
						fail(e) {
							reject(e);
						}
					})
				})
			} else {
				return new Promise((resolve, reject) => {
					reject("错误添加")
				})
			}
		},
	}
}();


export default UseSqliteFun;